package com.yueya.api.notice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.notice.model.NoticeStatus;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;

import net.sf.json.JSONObject;

public class INoticeService extends BaseService {

	// 获取列表
	public Map<String, Object> getPageList(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "pgn", "pgs", "userid")) {

			if (jo.containsKey("pgn")) {
				pageNumber = jo.getInt("pgn");
			}
			if (jo.containsKey("pgs")) {
				pageSize = jo.getInt("pgs");
			}

			String userId = jo.getString("userid");

			Page<Record> page = null;
			StringBuffer sb = new StringBuffer();
			String select = "";
			if (userId.equals("0001")) {
				select = "select a.*,b.user_name username,c.dept_name deptname,"
						+ "(select count(s.id) as count from t_notice_status s where s.user_id = '" + map.get("userid")
						+ "' and s.notice_id = a.id ) as readstatus";
				sb.append(
						" from t_notice  a left join t_user b on a.userid=b.user_id left join t_dept c on a.deptid=c.dept_id where a.delflag='0' and a.status = '1'");

				// 标题
				if (jo.containsKey("title")) {
					String title = jo.getString("title");
					if (StrKit.notBlank(title)) {
						sb.append(" and ( a.title like '%" + title + "%'  )");
					}
				}

				sb.append(" order by a.credat desc ");
			} else {

				select = "select * ";
				sb.append(
						" from ( select b.id id,b.title title,b.pubdat pubdat,b.content_text contenttext,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
								+ userId + "' and s.notice_id = b.id) as readstatus  ");
				sb.append(
						" from  t_notice b left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id ");
				sb.append(" where b.delflag='0' and b.type='0' and b.status='1' ");
				// 标题
				if (jo.containsKey("title")) {
					String title = jo.getString("title");
					if (StrKit.notBlank(title)) {
						sb.append(" and b.title like '%" + title + "%' ");
					}
				}
				sb.append(" union  ");
				sb.append(
						" select b.id id,b.title title,b.pubdat pubdat,b.content_text contenttext,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
								+ userId + "' and s.notice_id = b.id) as readstatus  ");
				sb.append(
						" from  t_notice_user a left join t_notice b on a.notice_id=b.id left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id  ");
				sb.append(" where a.user_id='" + userId + "' and b.status='1' and  b.delflag='0'   ");
				// 标题
				if (jo.containsKey("title")) {
					String title = jo.getString("title");
					if (StrKit.notBlank(title)) {
						sb.append(" and b.title like '%" + title + "%' ");
					}
				}
				sb.append(" union  ");
				sb.append(
						" select b.id id,b.title title,b.pubdat pubdat,b.content_text contenttext,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
								+ userId + "' and s.notice_id = b.id) as readstatus  ");
				sb.append(
						" from  t_notice_dept a left join t_notice b on a.notice_id=b.id left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id  ");
				sb.append(" where a.dept_id = (select du.dept_id from t_dept_user du where du.user_id='" + userId
						+ "') and b.status='1' and  b.delflag='0' ");
				// 标题
				if (jo.containsKey("title")) {
					String title = jo.getString("title");
					if (StrKit.notBlank(title)) {
						sb.append(" and b.title like '%" + title + "%' ");
					}
				}
				sb.append("  ) tm order by tm.pubdat desc ");
			}

			page = Db.paginate(pageNumber, pageSize, select, sb.toString());

			if (page != null) {
				map.put("result", true);
				List<Record> list = page.getList();
				Record r = null;
				for (int i = 0; i < list.size(); i++) {
					r = list.get(i);

					// 发布时间格式化
					String pubdat = r.getStr("pubdat");
					if (StrKit.notBlank(pubdat)) {
						r.set("pubdat", DateUtil.betweenBeforeTime(pubdat));
					}

				}
				map.put("data", list);
				map.put("size", page.getTotalRow());
				map.put("pgn", page.getPageNumber());
				map.put("pgs", page.getPageSize());
				map.put("totalPage", page.getTotalPage());
			} else {
				map.put("result", true);
				map.put("size", 0);
			}
		} else {
			map.put("result", false);
			error.put("errcode", "YY.INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}
		return map;
	}

	// 获取明细
	public Map<String, Object> getDetail(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "noticeid", "userid")) {
			String noticeId = jo.getString("noticeid");
			String userId = jo.getString("userid");
			String sql = "select b.id id,b.title title,b.pubdat pubdat,b.content_text contenttext,d.dept_name deptname from t_notice b left join t_dept d on b.deptid=d.dept_id where id = ?";
			Record notice = Db.findFirst(sql, noticeId);
			if (notice != null) {
				NoticeStatus.dao.saveReadStatus(noticeId, userId);
				map.put("result", true);
				map.put("data", notice);
			} else {
				map.put("result", false);
				error.put("errcode", "NO_RECORD");
				error.put("errmsg", "操作失败");
				map.put("error", error);
			}

		} else {
			map.put("result", false);
			error.put("errcode", "INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}

		return map;
	}

	// 设置为已读
	public Map<String, Object> setReaded(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "noticeid", "userid")) {
			String noticeId = jo.getString("noticeid");
			String userId = jo.getString("userid");
			boolean bl = NoticeStatus.dao.saveReadStatus(noticeId, userId);
			if (bl) {
				map.put("result", true);
			} else {
				map.put("result", false);
				error.put("errcode", "NO_RECORD");
				error.put("errmsg", "操作失败");
				map.put("error", error);
			}

		} else {
			map.put("result", false);
			error.put("errcode", "INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}

		return map;
	}

	// 获取已读用户列表
	public Map<String, Object> getReadedUserList(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "noticeid")) {
			String noticeId = jo.getString("noticeid");
			StringBuffer sql = new StringBuffer(
					" select ns.notice_id noticeid,u.user_id userid,u.user_name username,(select ub.avatar_path from t_user_base ub where ub.user_id = u.user_id) avatarpath,ns.create_time createtime ");
			sql.append(" from t_notice_status ns left join t_user u on u.user_id = ns.user_id where ns.notice_id = ? ");
			sql.append(" order by ns.create_time desc");

			List<Record> userList = Db.find(sql.toString(), noticeId);

			if (userList != null) {
				map.put("result", true);
				map.put("data", userList);
				map.put("size", userList.size());
			} else {
				map.put("result", true);
				map.put("size", 0);
			}
		} else {
			map.put("result", false);
			error.put("errcode", "YY.INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}
		return map;
	}

	public Map<String, Object> getUnreadNum(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "userid")) {
			String userId = jo.getString("userid");
			StringBuffer sql = new StringBuffer("select * ");
			sql.append(
					" from ( select b.id id,b.title title,b.credat credat,b.pubdat pubdat,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
							+ userId + "' and s.notice_id = b.id) as readstatus  ");
			sql.append(
					" from  t_notice b left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id ");
			sql.append(" where b.delflag='0' and b.type='0' and b.status='1' ");
			sql.append(" union  ");
			sql.append(
					" select b.id id,b.title title,b.credat credat,b.pubdat pubdat,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
							+ userId + "' and s.notice_id = b.id) as readstatus  ");
			sql.append(
					" from  t_notice_user a left join t_notice b on a.notice_id=b.id left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id  ");
			sql.append(" where a.user_id='" + userId + "' and b.status='1' and  b.delflag='0'  ");
			sql.append(" union  ");
			sql.append(
					" select b.id id,b.title title,b.credat credat,b.pubdat pubdat,c.user_name username,d.dept_name deptname,(select count(s.id) as count from t_notice_status s where s.user_id='"
							+ userId + "' and s.notice_id = b.id) as readstatus  ");
			sql.append(
					" from  t_notice_dept a left join t_notice b on a.notice_id=b.id left join t_user c on b.userid=c.user_id left join t_dept d on b.deptid=d.dept_id  ");
			sql.append(" where a.dept_id = (select du.dept_id from t_dept_user du where du.user_id='" + userId
					+ "') and b.status='1' and  b.delflag='0'  ) tm where tm.readstatus = '0' order by tm.pubdat desc, tm.credat desc ");

			List<Record> notice = Db.find(sql.toString());
			if (notice != null) {
				map.put("result", true);
				map.put("data", notice.size());
			} else {
				map.put("result", false);
				error.put("errcode", "NO_RECORD");
				error.put("errmsg", "操作失败");
				map.put("error", error);
			}

		} else {
			map.put("result", false);
			error.put("errcode", "INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}

		return map;
	}
}
